//
//  DynamicCell.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/30.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "DynamicCell.h"
#import "ZXChatHelper.h"
@implementation DynamicCell{
    UIImageView     *headImageV;
    QXLable         *labName;
    QXLable         *labContent;
    QXLable         *labReply;
    UIView          *replyView;
    QXLable         *labSource;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        headImageV=[[UIImageView alloc]init];
        headImageV.layer.masksToBounds=YES;
        headImageV.layer.cornerRadius=20;
        [self.contentView addSubview:headImageV];
        [headImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.contentView).with.offset(10);
            make.size.mas_equalTo(CGSizeMake(50, 50));
        }];
        
        labName=[[QXLable alloc]initTitle];
        [self.contentView addSubview:labName];
        [labName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headImageV.mas_right).with.offset(10);
            make.centerY.equalTo(headImageV);
        }];
        
        labContent=[[QXLable alloc]initTitle];
        [self.contentView addSubview:labContent];
        [labContent mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headImageV);
            make.right.equalTo(self.contentView).with.offset(-10);
            make.top.equalTo(headImageV.mas_bottom).with.offset(10);
        }];
        
        replyView =[[UIView alloc]init];
        replyView.backgroundColor=DEFAULTBGCOLOR;
        [self.contentView addSubview:replyView];
        [replyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(labContent);
            make.top.equalTo(labContent.mas_bottom).with.offset(10);
        }];
        
        labReply=[[QXLable alloc]init];
        [replyView addSubview:labReply];
        [labReply mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(replyView);
        }];
        
        labSource=[[QXLable alloc]initWithFount:10];
        [self.contentView addSubview:labSource];
        [labSource mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headImageV);
            make.top.equalTo(replyView.mas_bottom).with.offset(10);
        }];
        
        
        UIView *line=[[UIView alloc]init];
        line.backgroundColor=DEFAULTBGCOLOR;
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.contentView);
            
            make.top.equalTo(labSource.mas_bottom).with.offset(10);
            make.height.mas_equalTo(@5);
        }];
    }
    return self;
}

- (void)setModel:(DynamicModel *)model{
    _model=model;
    [headImageV sd_setImageWithURL:[NSURL URLWithString:model.Avater]];
    NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n%@",model.NickName,model.SubTime]];
    [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(attStr.length-model.SubTime.length, model.SubTime.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:DEFAULTTEXTGRAYCOLOR range:NSMakeRange(attStr.length-model.SubTime.length, model.SubTime.length)];
    labName.attributedText=attStr;
    labContent.attributedText=[ZXChatHelper formatMessageString:model.Content];
    if (model.ParentComment.Content.length>0) {
        labReply.attributedText=[ZXChatHelper formatMessageString:[NSString stringWithFormat:@"%@:n%@",model.ParentComment.NickName,model.ParentComment.Content]];
        
        [labReply mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(replyView).with.insets(UIEdgeInsetsMake(15, 20, 19, 10));
        }];
        NSMutableAttributedString *attStr1=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@发布于 %@ 计划",model.ParentComment.NickName,model.ParentComment.LotteryTypeName]];
        [attStr1 addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(attStr1.length-model.LotteryTypeName.length-3, model.LotteryTypeName.length+3)];
        labSource.attributedText=attStr1;
    }else{
        labReply.attributedText=nil;
        [labReply mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(replyView);
        }];
        
        NSMutableAttributedString *attStr1=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"他发布于 %@ 计划",model.LotteryTypeName]];
        [attStr1 addAttribute:NSForegroundColorAttributeName value:NAVCOLOR range:NSMakeRange(attStr1.length-model.LotteryTypeName.length-3, model.LotteryTypeName.length+3)];
        labSource.attributedText=attStr1;
        
    }
    
}

@end
